const app = getApp()
var flag = false;
Page({
  data: {
    userMessages:"",
    boardList:""



   
  },
  
  onLoad: function (options) {
    
  },

  


  onShow: function (options) {
    var that = this
    var openid = wx.getStorageSync("openid");
    console.log("openid", openid)
    wx.request({
      url: app.globalData.baseUrl +'/board/findmessagesbyopenid',
      method: 'GET',
      data: { "buyerOpenid": openid },//传入的参数
      success: function (res) {
        var list = res.data.boardList;
        if (list[0].boards[0].boardId!=null){
        var boardId0 = list[0].boards[0].boardId
        //var boardId1 = list[0].boards[1].boardId
        console.log("boardId0", boardId0)
        //console.log("boardId1", boardId1)
        console.log("list", list)
        let length = list[0].boards.length;
        for (let i = 0; i < length - 1; i++){
          for (let j = i + 1; j < length; j++){
            if (list[0].boards[i].boardId < list[0].boards[j].boardId) {
              let tem = list[0].boards[i];
              list[0].boards[i] = list[0].boards[j];
              list[0].boards[j] = tem;
            }
            }
        }

          if (list == null) {
            var toastText = '获取数据失败' + res.data.errMsg;
            wx.showToast({
              title: toastText,
              icon: 'none',
              duration: 2000
            });
          } else {
            that.setData({
              boardList: list


            });
          }

        }

        /*if (list == null) {
          var toastText = '获取数据失败' + res.data.errMsg;
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            boardList:list
            

          });
        }*/
      }
    })
  },


  //点击立即预约
  formSubmit: function (e) {
    var that = this;
    var warn = "";//弹框时提示的内容
    var flag = true;//判断信息输入是否完整
    //判断
    if (e.detail.value.userMessages == "") {
      warn = "留言不能为空";
    }else {

      flag = false;//若必要信息都填写，则不用弹框，且页面可以进行跳转
      

      wx.request({
        url: app.globalData.baseUrl +'/board/messagesinsert',
        data: {//获取数据传到后台
          buyerOpenid: wx.getStorageSync("openid"),
          userMessages: e.detail.value.userMessages
        
        },
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          var result = res.data.success
          var toastText = "操作成功！";
          if (result != true) {
            toastText = "操作失败" + res.data.errMsg;
          }
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });

          that.onShow()
          
        }
        
      })
      that.setData({
        userMessages:""
        
      });
      

      

    }
    //如果信息填写不完整，弹出输入框
    if (flag == true) {
      wx.showModal({
        title: '提示',
        content: warn
      })



    }


  },

  // 下拉刷新
  onPullDownRefresh: function () {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    var that = this;
    that.setData({
      boardList: null
    });
    setTimeout(function () {
      that.onShow()
      // 隐藏导航栏加载框
      wx.hideNavigationBarLoading();
      // 停止下拉动作
      wx.stopPullDownRefresh();
    }, 1500);

  },
})